<?php
namespace  app\project\controller;
use app\admin\model\Admin_model;
use think\Controller;
use think\Db;
use think\Request;

Class Advertmanger extends Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('fun',$fun);
            $this->assign('active','广告管理');
        }else{
            $this->redirect('index/login/index');
        }
    }
    public function mangeradv(){
        $this->assign('title','广告计划管理');
        return $this->fetch();
    }
    public function  plan_manage(){
        $where=array();
        $user=session('user');
        $data=Db::name('adv_plan')->where($where)->select();
        $audit=array(0=>'待审核',1=>'待投放',2=>'未通过审核',3=>'投放中');
        $pay_type=array(0=>'CPM',1=>'CPC');
        $budget_type=array(0=>'总预算', 1=>'日预算');
        $styles=array();
        $styles[0]='';
        $adv_style=Db::name('adv_style')->select();

        foreach ($adv_style as $style){
            $styles[$style['id']]=$style['style_name'];
        }
        foreach ($data as &$k){
            if($k['audit_state']==0){
                $k['operation']='<div style="width: 100px;"><a href="'.url('audit_plan').'?id='.$k['id'].'" class="btn btn-info">审核计划</a></div> ';
            }else  if($k['audit_state']==1 || $k['audit_state']==3){
                $k['operation']='<div style="width: 100px;"><a onclick="cancel_audit('.$k['id'].')" class="btn btn-danger">取消审核</a></div> ';
            }else{
                $k['operation']='<div style="width: 100px;"><a href="'.url('audit_plan').'?id='.$k['id'].'" class="btn btn-warning">重新审核</a></div> ';
            }
            $k['audit_state']=$audit[$k['audit_state']];
            $k['style']=$styles[$k['style']];
            $k['pay_type']=$pay_type[$k['pay_type']];
            $k['budget_type']=$budget_type[$k['budget_type']];
            if($k['put_type']==0){
                $k['put_end_time']='长期投放';
            }
        }
        return array('rows'=>$data,'total'=>count($data));
    }
    public function  audit_plan(){
        $plan_id=$_GET['id'];
        $plan=Db::name('adv_plan')->where('id',$plan_id)->find();
        $this->assign('plan',$plan);
        $user=session('user');
        //获取广告组与广告主
        $group= Db::name('adv_group')->select();
        $advertisers=Db::name('advertisers')->alias('a')->field('a.*,b.id as user_id')
            ->join('user b','a.name=b.username')->select();
        $this->assign('advertisers',$advertisers);
        $this->assign('group',$group);
        //获取广告分类
        $industry= Db::name('industry')->where('level',1)->select();
        $this->assign('industry',$industry);
        //获取自定义渠道
        $channel=Db::name('channel')->select();
        $this->assign('channel',$channel);
        //获取指定自定义手机品牌
        $brand=Db::name('phone_brand')->select();
        $this->assign('brand',$brand);
        //获取指定阅读兴趣定向
        $interest=Db::name('interest')->select();
        $this->assign('interest',$interest);
        //获取app自定义定向
        $app_custom=Db::name('app_custom')->select();
        $this->assign('app_custom',$app_custom);
        //获取按app定向
        $apps=Db::name('app')->select();
        $this->assign('apps',$apps);
        //获取自定义安装应用偏好
        $apply_bias=Db::name('apply_bias')->select();
        $this->assign('apply_bias',$apply_bias);
        //获取自定义广告点击偏好
        $click_bias=Db::name('click_bias')->select();
        $this->assign('click_bias',$click_bias);
        //获取广告样式
        $style=Db::name('adv_style')->select();
        $this->assign('style',$style);

        $this->assign('title','审核计划');
        return $this->fetch();
    }
    public function audit(){
        $data=input('post.');
        if ($data){
           $info= Db::name('adv_plan')->where('id',$data['id'])->update($data);
           if($info){
               if($data['audit_state']==1){
                   $plan=Db::name('adv_plan')->where('id',$data['id'])->find();
                   $statistical=array('adv_plan_id'=>$plan['id']
                   ,'attr_adv_group_id'=>$plan['attr_adv_group_id']);
                   if(Db::name('adv_statistical')->where($statistical)->find()){

                   }else{
                       $id=Db::name('adv_statistical')->insertGetId($statistical);

                   }
               }


               return array('code'=>1,'msg'=>'审核完成','url'=>url('mangeradv'));
           }else{
               return array('code'=>0,'msg'=>'审核失败');
           }
        }else{
            return array('code'=>0,'msg'=>'参数丢失');
        }
    }
    public function cancel_audit(){

        if (isset($_GET['id'])){
            $info=Db::name('adv_plan')->where('id',$_GET['id'])->update(array('audit_state'=>0,'audit_note'=>''));
            if($info){
                return array('code'=>1,'msg'=>'取消审核成功');
            }else{
                return array('code'=>0,'msg'=>'取消审核失败');
            }
        }else{
            return array('code'=>0,'msg'=>'参数丢失');
        }
    }
}